<?php
class articleController extends RPHP_Controller
{

    //权限前缀
    protected $_pvprefix = "article";

    //文章列表
    public function alist()
    {

        $priv = array($this->_pvprefix . "_ls");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }

            }

            if ($_GET['caid'] != "") {
                $caid = intval($_GET['caid']);
                if ($caid > 0) {
                    $para['caid'] = $caid;
                }

            }

            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }

            $res              = $model->getArticles($para, $page);
            $this->view->data = $res;

            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }

            $this->view->cate = $model->getCategory();
            // print_r($this->view->cate);

            //新增权限
            $this->view->pv['add'] = $model->checkPriv($this->_pvprefix . "_ad", false);
            //编辑权限
            $this->view->pv['edit'] = $model->checkPriv($this->_pvprefix . "_ed", false);
            //库存权限
            $this->view->pv['stock'] = $model->checkPriv($this->_pvprefix . "_st", false);
            //删除权限
            $this->view->pv['del'] = $model->checkPriv($this->_pvprefix . "_de", false);

            $this->display();

        }

    }

    //新增文章
    public function add()
    {

        $priv = array($this->_pvprefix . "_ad");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            $this->view->cate         = $model->getCategory();
            $this->view->uploadImages = array(
                array('name' => '封面图'),

            );
            $this->display();
        }

    }

    //编辑文章
    public function edit()
    {
        $priv = array($this->_pvprefix . "_ed");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $article_id = intval($_GET['article_id']);
            if ($article_id > 0) {
                $data = $model->getSingleArticle($article_id);
            }

            if ($data) {
                $this->view->auth = $auth;
                //文章图片
                $this->view->data = $data;
                $uploadImages     = array(
                    array('name' => '封面图'),
                );

                if ($data['pic_docids'] != '') {
                    $docids = explode(",", $data['pic_docids']);
                    if (count($docids) > 0) {
                        foreach ($docids as $key => $id) {
                            $uploadImages[$key]['file'] = $model->getNewDocs($id);
                        }
                    }
                }

                $this->view->uploadImages = $uploadImages;
                $this->view->cate         = $model->getCategory();
                $this->display();

            } else {
                RPHP_RR::nodata();
            }

        }

    }

    public function ajax()
    {

        $priv         = array($this->_pvprefix . "_ls");
        $model        = $this->model;
        $auth         = $model->checkPriv($priv, true, false);
        $data['code'] = 901;
        if ($auth) {
            $data['code'] = 900;
            if ($_POST['act'] == "getattr") {

                $data = $model->getFormAttr($_POST['typeid']);

            } elseif ($_POST['act'] == "remove") {
                $article_id = $_POST['article_id'];
                if ($article_id > 0) {
                    $model->removeArticle($article_id);
                }
                $data['code'] = 200;
            }

        }

        echo json_encode($data);

    }

    public function savearticle()
    {
        $priv = array($this->_pvprefix . "_ls");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->code = 900;
            set_time_limit(0);
            if ($_POST['act'] == "add" && $model->checkPriv($this->_pvprefix . "_ad", false)) {
                $this->view->success = "新增文章成功";
                $this->view->link    = array(
                    array('name' => '继续新增文章', 'url' => '/article/add'),
                    array('name' => '返回文章列表', 'url' => '/article/alist'),
                );

                $this->view->code = $model->saveArticle($_POST, $_FILES);

            } elseif ($_POST['act'] == "edit" && $model->checkPriv($this->_pvprefix . "_ed", false)) {

                $this->view->success = "编辑文章成功";
                $this->view->link    = array(
                    array('name' => '返回文章列表', 'url' => '/article/alist/?t=' . time()),
                );
                $this->view->code = $model->saveArticle($_POST, $_FILES, $_POST['article_id']);
            }

            $this->displaydo();

        }
    }

    //分类列表
    public function classlist()
    {

        $priv = array($this->_pvprefix . "_cla");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            //获取类型数据
            $this->view->data = $model->getCategory();
            //获取新增权限
            $this->view->pvadd = $model->checkPriv($this->_pvprefix . "_claad", false);
            //获取编辑权限
            $this->view->pvedit = $model->checkPriv($this->_pvprefix . "_claed", false);

            $this->display();

        }

    }

    //新增分类
    public function classadd()
    {
        $priv = array($this->_pvprefix . "_claad");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            //获取分类数据
            $this->view->data = $model->getCategory();
            $this->display();

        }

    }

    //编辑分类
    public function classedit()
    {

        $priv = array($this->_pvprefix . "_claed");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $caid             = intval($_GET['caid']);
            $cate             = $model->getCategory($caid);

            //商品图片
            if ($cate['docid'] > 0) {
                $docid = $cate['docid'];
            }

            if ($docid) {
                $cate['docid'] = $model->getDocs($docid);
            }

            if ($cate) {
                $this->view->cate                            = $cate;
                $this->view->stat[$this->view->cate['stat']] = ' checked';
                //获取类型数据
                $this->view->data = $model->getCategory();
                $this->display();
            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //分类排序
    public function classsort()
    {

        $priv = array($this->_pvprefix . "_caed");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->data = $model->getCategory(intval($_GET['caid']), true);
            $this->display();

        }

    }

    //分类操作
    public function classdo()
    {

        $priv = array($this->_pvprefix . "_cla");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->code = 900;

            if ($_POST['act'] == "add" && $model->checkPriv($this->_pvprefix . "_claad", false, false)) {

                $this->view->success = "新增分类成功";
                $this->view->link    = array(
                    array('name' => '继续新增分类', 'url' => '/article/classadd'),
                    array('name' => '返回分类列表', 'url' => '/article/classlist'),
                );
                $this->view->code = $model->saveCategory($_POST, $_FILES);

            } elseif ($_POST['act'] == "edit" && $model->checkPriv($this->_pvprefix . "_claed", false, false)) {

                $this->view->success = "编辑分类成功";
                $this->view->link    = array(
                    array('name' => '返回分类列表', 'url' => '/article/classlist'),
                );
                $this->view->code = $model->saveCategory($_POST, $_FILES, $_POST['caid']);

            } elseif ($_POST['act'] == "sort" && $model->checkPriv($this->_pvprefix . "_claed", false, false)) {

                $this->view->success = "保存分类排序成功";
                $this->view->link    = array(
                    array('name' => '返回分类列表', 'url' => '/article/classlist'),
                );
                $this->view->code = $model->saveCategorySort($_POST);

            }

            $this->displaydo();

        }

    }

    //执行图片上传
    public function upload_do()
    {
        $data['code'] = 900;
        $priv         = array($this->_pvprefix . "_ls");
        $model        = $this->model;
        $auth         = $model->checkPriv($priv, true, false);
        if ($auth) {

            $data = $model->UploadPic($_FILES['files']);

        }

        $this->view->data = $data;

        $this->display();
    }

    //上传图片
    public function upload()
    {
        $priv  = array($this->_pvprefix . "_ls");
        $model = $this->model;
        $auth  = $model->checkPriv($priv, true, false);
        if ($auth) {
            $this->display();
        }
    }

}
